package com.symaster.common.swing.message;

import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;
import com.symaster.common.swing.style.Style;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class MessageBox extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JTextField message_body;
    private JCheckBox not_show_button;
    private final Integer width = 500;
    private final Integer height = 150;
    private boolean isOk = false;
    private boolean notShow = false;

    public MessageBox(String title, String body, Boolean showNotShowButton) {
        initPage(title, showNotShowButton);
        initData(body);
        initListener();
    }

    public MessageBox(String title, String body) {
        initPage(title, true);
        initData(body);
        initListener();
    }

    private void initData(String body) {
        message_body.setText(body);
    }

    private void initPage(String title, Boolean showNotShowButton) {

        Dimension dimension = new Dimension(width, height);
        Dimension buttonDimension = new Dimension(130, 22);
        Point point = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();

        Style.setBorder(1, buttonCancel, buttonOK, message_body, not_show_button);
        Style.setFont(1, buttonCancel, buttonOK, message_body, not_show_button);

        this.not_show_button.setVisible(showNotShowButton);
        this.buttonOK.setPreferredSize(buttonDimension);
        this.buttonCancel.setPreferredSize(buttonDimension);
        this.setMinimumSize(dimension);
        this.setPreferredSize(dimension);
        this.setTitle(title);
        this.setContentPane(contentPane);
        this.setModal(true);
        this.getRootPane().setDefaultButton(buttonOK);
        this.setBounds(point.x - width / 2, point.y - height / 2, width, height);
        this.pack();
    }

    private void initListener() {
        this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });
        buttonOK.addActionListener(e -> onOK());
        buttonCancel.addActionListener(e -> onCancel());
        contentPane.registerKeyboardAction(e -> onCancel(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
        not_show_button.addActionListener(e -> notShow = not_show_button.isSelected());
    }

    public ResultEntity open() {
        this.setVisible(true);
        ResultEntity resultEntity = new ResultEntity(isOk, notShow);
        return resultEntity;
    }

    private void onOK() {
        isOk = true;
        dispose();
    }

    private void onCancel() {
        // add your code here if necessary
        dispose();
    }

    {
        // GUI initializer generated by IntelliJ IDEA GUI Designer
        // >>> IMPORTANT!! <<<
        // DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        contentPane = new JPanel();
        contentPane.setLayout(new GridLayoutManager(2, 1, new Insets(10, 10, 10, 10), -1, -1));
        contentPane.setBackground(new Color(-14407378));
        contentPane.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), null, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new GridLayoutManager(1, 3, new Insets(0, 0, 0, 0), -1, -1));
        panel1.setBackground(new Color(-14407378));
        contentPane.add(panel1, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, 1, null, null, null, 0, false));
        panel1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), null, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
        final Spacer spacer1 = new Spacer();
        panel1.add(spacer1, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false));
        final JPanel panel2 = new JPanel();
        panel2.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1));
        panel2.setBackground(new Color(-14407378));
        panel1.add(panel2, new GridConstraints(0, 2, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        panel2.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), null, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
        buttonOK = new JButton();
        buttonOK.setBackground(new Color(-13024950));
        buttonOK.setFocusPainted(false);
        buttonOK.setForeground(new Color(-5915180));
        buttonOK.setText("OK");
        panel2.add(buttonOK, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        buttonCancel = new JButton();
        buttonCancel.setBackground(new Color(-13024950));
        buttonCancel.setFocusPainted(false);
        buttonCancel.setForeground(new Color(-5915180));
        buttonCancel.setText("Cancel");
        panel2.add(buttonCancel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        not_show_button = new JCheckBox();
        not_show_button.setBackground(new Color(-14407378));
        not_show_button.setFocusPainted(false);
        not_show_button.setForeground(new Color(-5915180));
        not_show_button.setText("不在提示");
        panel1.add(not_show_button, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final JPanel panel3 = new JPanel();
        panel3.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
        panel3.setBackground(new Color(-14407378));
        contentPane.add(panel3, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        panel3.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), null, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
        message_body = new JTextField();
        message_body.setBackground(new Color(-14407378));
        message_body.setEditable(false);
        message_body.setForeground(new Color(-5915180));
        panel3.add(message_body, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
    }

    /** @noinspection ALL */
    public JComponent $$$getRootComponent$$$() {
        return contentPane;
    }

    public class ResultEntity {
        private Boolean isOk;
        private Boolean notShow;

        public ResultEntity() {
        }

        public ResultEntity(Boolean isOk, Boolean notShow) {
            this.isOk = isOk;
            this.notShow = notShow;
        }

        public Boolean getOk() {
            return isOk;
        }

        public void setOk(Boolean ok) {
            isOk = ok;
        }

        public Boolean getNotShow() {
            return notShow;
        }

        public void setNotShow(Boolean notShow) {
            this.notShow = notShow;
        }

        @Override
        public String toString() {
            return "ResultEntity{" +
                    "isOk=" + isOk +
                    ", notShow=" + notShow +
                    '}';
        }
    }
}
